With an alpha color mixing method presently applied widely in the field of computer graphics, any imaginable translucent effect, such as dazzling effect of fire, smoke, a shadow, a dynamic light source, and the like, may be implemented.
The alpha color mixing method in image processing serves to implement a sort of translucent effect. Given a pixel A of an opaque object and a pixel B of another transparent object, when pixel A is viewed through pixel B, a pixel C observed by an observer is a pixel resulted from a mixture of pixel B and pixel A. For example, a mixture of 30% of pixel B and 70% of pixel A gives a translucent pixel C of 100%, wherein a pixel (also known as a picture element) is a fundamental unit in image displaying.
Steps of implementing a traditional alpha color mixing method are as follows.
Step 1: pixel A is decomposed into RGB color components denoted by R(A), G(A), and B(A).
Step 2: pixel B is decomposed into RGB color components denoted by R(B), G(B), and B(B).
Step 3: a transparency alpha of pixel B is acquired (wherein the alpha falls in the range of a floating-point number [0.0, 1.0], wherein a pixel is completely transparent with alpha=0.0, and a pixel is completely opaque with alpha=1.0).
Step 4: as the sum of the transparencies of pixel A and pixel B is the maximal value of a transparency, the acquired transparency of pixel A is 1.0-alpha.
Step 5: a mixed pixel C is formed by a mixture of pixel B and pixel A, and RGB color components of the mixed pixel C, denoted by R(C), G(C), and B(C), are calculated respectively.
RGB color component R(C) of the mixed pixel C equals the sum of the product of color component R(B) of pixel B times the transparency alpha, and the product of color component R(A) of pixel A times the transparency 1.0-alpha.
RGB color component G(C) of the mixed pixel C equals the sum of the product of color component G(B) of pixel B times the transparency alpha, and the product of color component G(A) of pixel A times the transparency 1.0-alpha.
RGB color components B(C) of the mixed pixel C equals the sum of the product of color component B(B) of pixel B times the transparency alpha, and the product of color component B(A) of pixel A times the transparency 1.0-alpha.
Step 6: color components R(C), G(C), and B(C) of the mixed pixel C are synthesized into the final mixed pixel C.
Adopting of the alpha color mixing method has a major impact on screen-refreshing performance, as each time a screen is refreshed, each pixel on the screen has to be calculated using the method. For example, a currently common wide-screen display of a computer generally has a resolution of 1440*900 (i.e., 1296000) pixels, and therefore with the alpha color mixing method, a number of 1296000 calculations have to be performed each time the screen is refreshed.
For example, Chinese patent application with publication No. CN1825353, titled MIXING MODE FOR HARDWARE ACCELERATION, discloses a solution for mixing colors of a source graphic-element and a target graphic-element using a graphic processing unit, wherein the graphic processing unit receives from a central processing unit or a graphic application a mixing program for mixing said graphic-elements and executes the mixing program. For example, the graphic processing unit draws the source graphic-element in a source texture map, and draws the target graphic-element in a target texture map. The mixing program (for example, a pixel shader) is configured to be applied to said graphic-elements, and the graphic processing unit applies a mixing mode to each pixel of said graphic-elements so as to present a synthetic graphic-element in a target to be presented.
This solution is disadvantageous as it uses the aforementioned traditional alpha color mixing method, wherein each pixel to be mixed is first decomposed into RGB color components respectively; then mixing calculation is performed on each color component; finally mixed RGB color components are synthesized into a final mixed pixel, such that with the method, massive calculations have to be performed, leading to low efficiency.
In view of this, at present technical solutions as follow are also adopted to achieve the goal of alpha color mixing.
With technical solution 1, a fixed-point-number method is adopted.
With the prior alpha color mixing method, the alpha of a floating-point number is used. However, calculation with fixed-point numbers is normally much faster than calculation with floating-point numbers. For example, the 8-bit fixed-point number of a floating-point number 1.0 is 255, so the traditional alpha color mixing method may be improved to be implemented with the following steps.
Step 1: pixel A is decomposed into RGB color components denoted by R(A), G(A), and B(A).
Step 2: pixel B is decomposed into RGB color components denoted by R(B), G(B), and B(B).
Step 3: a transparency alpha of pixel B is acquired (wherein the alpha falls in the range of a fixed-point number [0, 255], wherein a pixel is completely transparent with alpha=0, and a pixel is completely opaque with alpha=255).
Step 4: as the sum of the transparencies of pixel A and pixel B is the maximal value of a transparency, the acquired transparency of pixel A is 255-alpha.
Step 5: a mixed pixel C is formed by a mixture of pixel B and pixel A, and RGB color components of the mixed pixel C, denoted by R(C), G(C), and B(C), are calculated respectively.
RGB color component R(C) of the mixed pixel C equals the sum of the product of color component R(B) of pixel B times the transparency alpha, and the product of color component R(A) of pixel A times the transparency 255-alpha.
RGB color component G(C) of the mixed pixel C equals the sum of the product of color component G(B) of pixel B times the transparency alpha, and the product of color component G(A) of pixel A times the transparency 255-alpha.
RGB color components B(C) of the mixed pixel C equals the sum of the product of color component B(B) of pixel B times the transparency alpha, and the product of color component B(A) of pixel A times the transparency 255-alpha.
Step 6: RGB color components R(C), G(C), and B(C) of the mixed pixel C are divided exactly by 255 respectively, and are then synthesized into the final mixed pixel C.
For aforementioned technical solution 1, as exact division is a relatively slow calculation, modification may be made to the aforementioned flow, for example by replacing exact division with bit-shifting commonly adopted at present.
In the aforementioned technical solution 1, levels of the transparency alpha fall in the range of [0, 255], which may be narrowed to [0, 32] as it is often unnecessary to use such refined levels of the transparency alpha as [0, 255] in a practical application. As moving to the right by 5 bits is equivalent to exact division by 32, aforementioned technical solution 1 may be improved as follows.
Step 1: pixel A is decomposed into RGB color components denoted by R(A), G(A), and B(A).
Step 2: pixel B is decomposed into RGB color components denoted by R(B), G(B), and B(B).
Step 3: a transparency alpha of pixel B is acquired (wherein the alpha falls in the range of a fixed-point number [0, 32], wherein a pixel is completely transparent with alpha=0, and a pixel is completely opaque with alpha=32).
Step 4: as the sum of the transparencies of pixel A and pixel B is the maximal value of a transparency, the acquired transparency of pixel A is 32-alpha.
Step 5: a mixed pixel C is formed by a mixture of pixel B and pixel A, and RGB color components of the mixed pixel C, denoted by R(C), G(C), and B(C), are calculated respectively.
RGB color component R(C) of the mixed pixel C equals the sum of the product of color component R(B) of pixel B times the transparency alpha, and the product of color component R(A) of pixel A times the transparency 32-alpha.
RGB color component G(C) of the mixed pixel C equals the sum of the product of color component G(B) of pixel B times the transparency alpha, and the product of color component G(A) of pixel A times the transparency 32-alpha.
RGB color components B(C) of the mixed pixel C equals the sum of the product of color component B(B) of pixel B times the transparency alpha, and the product of color component B(A) of pixel A times the transparency 32-alpha.
Step 6: RGB color components R(C), G(C), and B(C) of the mixed pixel C are moved to the right by 5 bits respectively, and are then synthesized into the final mixed pixel C.
With technical solution 2, a method by looking up a table is adopted.
the method by looking up a table is an optimizing method that saves time at the price of space. For example, a product of color component R(B) times the transparency alpha may be calculated beforehand, and stored in a 2D-array table; data in the 2D-array table are read directly each time color mixing is performed using the alpha color mixing method. It is assumed that the 2D-array table saves only a product of an RGB color component times the transparency alpha, such that resulting memory space occupation is small.
Based on that, the traditional alpha color mixing method may be improved as follows.
Step 1: pixel A is decomposed into RGB color components denoted by R(A), G(A), and B(A).
Step 2: pixel B is decomposed into RGB color components denoted by R(B), G(B), and B(B).
Step 3: a transparency alpha of pixel B is acquired, wherein the transparency falls in the range of an integer [0, 31], a pixel is completely transparent with alpha=0, and a pixel is completely opaque with alpha=31.
Step 4: as the sum of the transparencies of pixel A and pixel B is the maximal value of a transparency, the acquired transparency of pixel A is 31-alpha.
Step 5: a mixed pixel C is formed by a mixture of pixel B and pixel A, and RGB color components of the mixed pixel C, denoted by R(C), G(C), and B(C), are calculated respectively.
RGB color component R(C) of the mixed pixel C equals the sum of the product of color component R(B) of pixel B times the transparency alpha, and the product of color component R(A) of pixel A times the transparency 31-alpha, wherein values of both products are read from the 2D-array table directly.
RGB color component G(C) of the mixed pixel C equals the sum of the product of color component G(B) of pixel B times the transparency alpha, and the product of color component G(A) of pixel A times the transparency 31-alpha, wherein values of both products are read from the 2D-array table directly.
RGB color components B(C) of the mixed pixel C equals the sum of the product of color component B(B) of pixel B times the transparency alpha, and the product of color component B(A) of pixel A times the transparency 31-alpha, wherein values of both products are read from the 2D-array table directly.
Step 9: color components R(C), G(C), and B(C) of the mixed pixel C are synthesized into the final mixed pixel C.
With both technical solutions, to-be-mixed pixels are to be decomposed respectively into RGB color components, and then calculation is performed until a mixed pixel C is obtained finally. Wherein, with the fixed-point-number method, six integer multiplications have to be performed, thereby with a relatively long operating time; with the method by looking up a table, six integer multiplications are no longer required due to use of a 2D-array table, however it is required to traverse the 2D-array table to extracted data from the 2D-array table, which is time-consuming too, and what's more, more memory space is required for the 2D-array table. For example, for 16-bit RGB565 pixels, the range of levels of the alpha is taken as [0, 31], a 256*32 2D-array table is required, and memory occupation reaches 16 KB.